package com.yuan.model_convenient.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_convenient.domain.ConvenientReporting;
import com.yuan.model_convenient.repository.ConvenientReportingDao;
import com.yuan.model_convenient.service.ConvenientReportingService;
import com.yuan.model_convenient.service.dto.ConvenientReportingDto;
import com.yuan.model_convenient.service.mapstruct.ConvenientReportingMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
@RequiredArgsConstructor
public class ConvenientReportingServiceImpl implements ConvenientReportingService {

    private final ConvenientReportingDao convenientReportingDao;

    private final ConvenientReportingMapper convenientReportingMapper;

    @Override
    public PageResult<ConvenientReportingDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<ConvenientReporting> page = convenientReportingDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(convenientReportingMapper::toDto));
    }

    @Override
    public List<ConvenientReportingDto> queryAll(CommonQueryCriteria criteria) {
        List<ConvenientReporting> users = convenientReportingDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return convenientReportingMapper.toDto(users);
    }

    @Override
    public void download(List<ConvenientReportingDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ConvenientReportingDto convenientReportingDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("申请表Id", convenientReportingDto.getReportId());
            map.put("申请原因", convenientReportingDto.getReportReason());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ConvenientReporting resources) {
        convenientReportingDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConvenientReporting resources) throws Exception {
        ConvenientReporting convenientReporting = convenientReportingDao.findById(resources.getReportId()).orElseGet(ConvenientReporting::new);
        ValidationUtil.isNull(convenientReporting.getReportId(), "ConvenientReporting", "reportId", resources.getReportId());
        convenientReportingDao.save(convenientReporting);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        convenientReportingDao.deleteById(ids);
    }
}
